﻿using KinonekoSoftware.CodariaDB.Runtimes.Statistics;
using KinonekoSoftware.JuXiaoYouDB.Core;
using KinonekoSoftware.Studio.Galleries;
using KinonekoSoftware.Studio.Pages.Composes;
using KinonekoSoftware.Studio.Pages.Explore;
using KinonekoSoftware.Studio.Pages.Galleries;
using KinonekoSoftware.Studio.Pages.Tools;
using KinonekoSoftware.Studio.Utils;
using Verbose = KinonekoSoftware.Studio.Messages.Verbose;

// ReSharper disable UnusedMember.Local

namespace KinonekoSoftware.Studio.ViewModels
{
    public sealed partial class RepositoryGalleryViewModel : TabViewModelCore
    {
        private readonly IRepository _repository;

        private int _version;

        public RepositoryGalleryViewModel()
        {
            _repository = Platform.GetRepository();

            GotoInspirationCommand = CreateCommand(DoGotoInspirationCommand);
            GotoExploreCommand     = CreateCommand(DoGotoExploreCommand);
            GotoWVCCommand         = CreateCommand(DoGotoWVCCommand);
            GotoProjectCommand     = CreateCommand(DoGotoProjectCommand);
            GotoToolsCommand       = CreateCommand(DoGotoToolsCommand);
            GotoComposeCommand     = CreateCommand(DoGotoComposeCommand);
            GotoInstanceCommand    = CreateCommand(DoGotoInstanceCommand);
            GotoRelationCommand    = CreateCommand<Type>(DoGotoRelationCommand);

            IsSingleton = true;
            Removable   = false;
        }

        //-------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------

        private void RefreshAmountStates()
        {
            Platform.GetEngine<
                InstanceCacheEngine,
                TheoryEngine_Chikara,
                TheoryEngine_Element,
                TheoryEngine_Vocabulary,
                TheoryEngine_Rarity>(
                                     out _,
                                     out var e_instance,
                                     out var e_chikara,
                                     out var e_element,
                                     out var e_vocabulary,
                                     out var e_rarity);
            AmountOfInstance = e_instance.Count();
            AmountOfTheory   = e_chikara.Count() + e_element.Count() + e_vocabulary.Count() + e_rarity.Count();
            // public int Count() => InsideDB.Count();

            RaiseUpdated(nameof(AmountOfInstance));
            RaiseUpdated(nameof(AmountOfInspiration));
            RaiseUpdated(nameof(AmountOfCompose));
            RaiseUpdated(nameof(AmountOfTheory));
            RaiseUpdated(nameof(AmountOfStoryPoint));
            RaiseUpdated(nameof(AmountOfPlots));
            RaiseUpdated(nameof(AmountOfInspirationSegments));

            _version = e_instance.Version   +
                       e_chikara.Version    +
                       e_element.Version    +
                       e_vocabulary.Version +
                       e_rarity.Version;
        }

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------

        protected override async void OnStart()
        {
            await RepositoryManagerViewModel.EnsureRepositoryAsync(GoTo<InstanceGalleryViewModel>); 
            base.OnStart();
            RefreshAmountStates();
        }

        protected override void OnResume()
        {
            base.OnResume();
            RefreshAmountStates();
        }

        //-------------------------------------------------
        //
        //         Message Command Handlers
        //
        //-------------------------------------------------

        [MessageHandler(typeof(CommandMessage))]
        private void CommandMessageProc(MessageCore msg)
        {
            if (msg is not CommandMessage cmdMsg)
            {
                return;
            }

            if (cmdMsg.Verbose == Verbose.OpenSetting)
            {
                GoTo<SettingViewModel>();
            }
        }


        //-------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------

        /// <summary>
        /// 打开灵感列表页。
        /// </summary>
        private void DoGotoInspirationCommand()
        {
            GoTo<PlotsQuickEditorViewModel>();
        }


        /// <summary>
        /// 打开设定列表页。
        /// </summary>
        private void DoGotoInstanceCommand()
        {
            GoTo<InstanceGalleryViewModel2>();
        }

        /// <summary>
        /// 打开探索列表页。
        /// </summary>
        private void DoGotoExploreCommand()
        {
            GoTo<ExploreGalleryViewModel>();
        }


        /// <summary>
        /// 打开世界观列表页。
        /// </summary>
        private void DoGotoWVCCommand()
        {
            GoTo<WorldViewConfigTheoryViewModel>();
        }


        /// <summary>
        /// 打开企划列表页。
        /// </summary>
        private void DoGotoProjectCommand()
        {
            GoTo<ProjectGalleryViewModel>();
        }


        /// <summary>
        /// 打开工具列表页。
        /// </summary>
        private void DoGotoToolsCommand()
        {
            GoTo<ToolsGalleryViewModel>();
        }

        /// <summary>
        /// 打开写作列表页。
        /// </summary>
        private void DoGotoComposeCommand()
        {
            GoTo<ComposeGalleryViewModel>();
        }


        private void DoGotoRelationCommand(Type type)
        {
            if (type is null || !type.IsAssignableTo(typeof(TabViewModelCore)))
            {
                return;
            }
            
            GoTo(Activator.CreateInstance(type) as TabViewModelCore);
        }


        public int AmountOfInstance            { get; private set; }
        public int AmountOfInspiration         { get; private set; }
        public int AmountOfCompose             { get; private set; }
        public int AmountOfTheory              { get; private set; }
        public int AmountOfStoryPoint          { get; private set; }
        public int AmountOfPlots               { get; private set; }
        public int AmountOfInspirationSegments { get; private set; }

        //-------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------
        private static AppViewModel AppViewModel => Ioc.Get<AppViewModel>();

        public ICommandEX GotoSettingCommand           => AppViewModel.GotoSettingCommand;
        public ICommandEX GotoTutorialCommand          => AppViewModel.GotoTutorialCommand;
        public ICommandEX GotoBugReportCommand         => AppViewModel.GotoBugReportCommand;
        public ICommandEX RemoveAllNotificationCommand => AppViewModel.RemoveAllNotificationCommand;

        public ICommandEX GotoInstanceCommand    { get; }
        public ICommandEX GotoInspirationCommand { get; }
        public ICommandEX GotoExploreCommand     { get; }
        public ICommandEX GotoWVCCommand         { get; }
        public ICommandEX GotoToolsCommand       { get; }
        public ICommandEX GotoComposeCommand     { get; }
        public ICommandEX GotoProjectCommand     { get; }
        public ICommandEX GotoRelationCommand    { get; }

        public ObservableCollection<JuXiaoYouNotification> Notifications => AppViewModel.Notifications;
    }
}